package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"info/util"
	"io/ioutil"
	"net/http"
	URL "net/url"
	"strings"
)

func FinanceSinaStock() {
	query:="list=gb_dji,gb_ixic,gb_inx,znb_UKX,znb_DAX,znb_INDEXCF,znb_CAC,znb_SMI,znb_FTSEMIB,znb_MADX,znb_OMX,znb_HEX,znb_OSEAX,znb_ISEQ,znb_AEX,znb_IBEX,znb_SX5E,znb_XU100,znb_NKY,znb_TWSE,znb_FSSTI,znb_KOSPI,znb_FBMKLCI,znb_SET,znb_JCI,znb_PCOMP,znb_KSE100,znb_SENSEX,znb_VNINDEX,znb_CSEALL,znb_SASEIDX,znb_SPTSX,znb_MEXBOL,znb_IBOV,znb_MERVAL,znb_AS51,znb_NZSE50FG,znb_CASE,znb_JALSH"
	url:="http://hq.sinajs.cn/?"+query
	resp,er:=http.Get(url)
	if er!=nil{
		panic(er)
	}
	data,er:=ioutil.ReadAll(resp.Body)
	if er != nil {
		panic(er)
	}
	data,er=util.GbkToUtf8(data)
	if er != nil {
		panic(er)
	}
	fmt.Println(string(data))
}

func WallstreetPageIndex() {
	url := "https://wallstreetcn.com/live/global"
	doc, er := util.GetDocument(url)
	if er != nil {
		panic(er)
	}

	items := doc.Find(".columns-left .livenews-main>div>div.live-item")
	items.Each(func(i int, item *goquery.Selection) {
		a := item.ChildrenFiltered("a")
		href, exists := a.Attr("href")
		if !exists {
			return
		}
		href = strings.TrimSpace(href)
		id0 := href[(strings.LastIndex(href, "/") + 1):]
		fmt.Println(id0)
	})
}

type WallstreetResponse struct {
	Code    int32       `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type WallstreetLive struct {
	Items []struct {
		Article struct {
			ID        int      `json:"id"`
			ImageURI  string   `json:"image_uri"`
			Platforms []string `json:"platforms"`
			Title     string   `json:"title"`
			URI       string   `json:"uri"`
		} `json:"article"`
		Author struct {
			ID          int64  `json:"id"`
			DisplayName string `json:"display_name"`
			Image       string `json:"image"`
			URI         string `json:"uri"`
		} `json:"author"`
		Channels          []string `json:"channels"`
		Content           string   `json:"content"`
		ContentText       string   `json:"content_text"`
		DisplayTime       int      `json:"display_time"`
		GlobalChannelName string   `json:"global_channel_name"`
		GlobalMoreURI     string   `json:"global_more_uri"`
		Id                int      `json:"id"`
		ImageUris         []string `json:"image_uris"`
		Title             string   `json:"title"`
	} `json:"items"`
	NextCursor    string `json:"next_cursor"`
	PollingCursor string `json:"polling_cursor"`
}

func getWallstreetLive() {
	query := URL.Values{
		"channel":    []string{"global-channel"},
		"client":     []string{"pc"},
		"cursor":     []string{"1549588510"},
		"limit":      []string{"20"},
		"first_page": []string{"false"},
		"accept":     []string{"live,vip-live"},
	}
	url := "https://api-prod.wallstreetcn.com/apiv1/content/lives?" + query.Encode()
	fmt.Println(url)
	resp, er := http.Get(url)
	if er != nil {
		panic(er)
	}
	data, er := ioutil.ReadAll(resp.Body)
	if er != nil {
		panic(er)
	}
	fmt.Println(string(data))
	var response = WallstreetResponse{Data: WallstreetLive{}}
	er = util.FromJson(data, &response)
	if er != nil {
		panic(er)
	}
	fmt.Println(response.Data.(WallstreetLive))
}

func main() {
	FinanceSinaStock()
}
